package com.order.frontend.order.Entity.cs;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;

import java.util.concurrent.TimeUnit;

public class csThread implements Runnable{
    RedissonClient redissonClient;
    private String nickname;
    public csThread() {
        // 默认构造函数
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public csThread(String nickname, RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
        this.nickname = nickname;
    }
    @Override
    public void run() {
        boolean b= false;
        System.out.println("我是, " + nickname+"号线程");
        RLock lock = redissonClient.getLock("lock1");
        try {
            b = lock.tryLock(31000, -1, TimeUnit.MILLISECONDS);
            if (b) {
                System.out.println(nickname+"号线程获取到锁了开始删除订单");
                Thread.sleep(30000);
            }else {
                System.out.println(nickname+"号线程没有获取到锁");
            }

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            if(b){
                System.out.println(nickname+"号线程释放锁");
                lock.unlock();
            }

        }

        ;

    }
}
